<?php
class controller_index extends controller_base
{
	public function __construct()
    {
        parent::__construct();
        $this->_module_searchpicinfo	= new module_searchpicinfo();
        $this->_data_searchinfo			= new data_searchinfo();
        
        $this->upload_file_path = 'resources/images/search_pic/';
        $this->reveal_path		= 'http://renxun.net/resources/images/search_pic/';
        
        $this->pass = 'wangnan11915';
        //http://zhaoren.com/index.php?act=publish_list&t=wangnan11915
        
        //点击相差时间(单位：小时)
        $this->distance_time = 1;
        
        //移动照片位置
        $this->change_location = 10;
        
         //照片变动高度(单位：px)
        $this->change_pic_size_height = 10;
        
         //照片变动宽度(单位：px)
        $this->change_pic_size_width = 10;
    }
	
    public function index()
	{
		//发布
		if(isset($_GET['publish']) && $_GET['publish'] && isset($_GET['id']) && $_GET['id'])
		{
			$id = $_GET['id'];
				
			$update_data['is_publish']		= 1;
			$update_data['publish_date']	= date('Y-m-d H:i:s', time());
			
			if($this->_data_searchinfo->update_by_id($id, $update_data))
			{
				//发邮件通知
				$searchinfo = $this->_data_searchinfo->fetch_by_id($id, 'contact_email');
				
				$tomail			= $searchinfo['contact_email'];
//$tomail='594wangnan@sohu.com';
				$subject		= '您的“'.$searchinfo['search_title'].'”已经发布';
				$mail_contents	= '您发布的寻找信息已经在<a href="http://renxun.net/">http://renxun.net/</a>这里发布，请查看，点击照片可使您要找的人的照片放大或者位置前移。祝您早日团聚！';
				$this->send_email($tomail, $subject, $mail_contents);
	
				$publish_url = "/?act=publish_list&t=$this->pass";		
				echo "<script>alert('发布成功'); location.href='$publish_url'</script>";
			}
		}
		
		$searchinfo = $this->_data_searchinfo->get_search_info();		
	
		$this->_view->assign('reveal_path', $this->reveal_path);		
		$this->_view->assign('searchinfo', $searchinfo);
		
		$this->_view->assign('change_pic_size_height', $this->change_pic_size_height);
		$this->_view->assign('change_pic_size_width', $this->change_pic_size_width);
		$this->_view->assign('distance_time', $this->distance_time);
		
		$this->_view->assign('top_sy', 'active');
		
        $this->_view->display('index.html');
	}
    
    /* 记录点击图片
     * 成功返回1;失败返回2
     *
    */
	public function click_pic()
	{		
		$pic_id = str_replace('picid_', '', $_GET['pid']);
		
		$distance_time = $this->distance_time * 3600;
	
		if($this->_module_searchpicinfo->check_click($pic_id, $distance_time))
		{
			//放大相片
			if($_GET['type'] == 1)
			{
				$update_data['pic_height']	= $_GET['height'];
				$update_data['pic_width']	= $_GET['width'];
				
				if($this->_module_searchpicinfo->change_pic_size($pic_id, $update_data))
				{
					echo 1;
					exit;
				}
			}
			
			//移动照片位置
			elseif($_GET['type'] == 2)
			{
				if($this->_data_searchinfo->update_station($pic_id, $this->change_location))
				{
					echo 1;
					exit;
				}
			}
		}
		
		echo 2;	
	}
	
	//上传寻人资料
	public function upload_search_info()
	{
		if($_POST)
		{		
			unset($_POST['ok']);	
			if($this->_module_searchpicinfo->upload_search($_POST, $_FILES, $this->upload_file_path))
			{
				$tip = "上传寻找人资料成功，请等待审核";
			}
			else
			{
				$tip = "上传寻找人资料失败";
			}
			echo '<script>alert("'.$tip.'"); location.href="/"</script>';
		}
		
		$collect_info = array();
		if(isset($_GET['em']) && $email_md5 = $_GET['em'])
		{				
			$data_collectinfo = new data_collectinfo();
			
			$condition		= "email_md5 = '$email_md5'";			
			$collect_info	= $data_collectinfo->fetch_one_by_condition($condition);
		}
		
		$this->_view->assign('collect_info', $collect_info);
		$this->_view->assign('top_fb', 'active');
		
		$this->_view->display('upload_search_info.html');		
	}
	
	//寻找人信息详情
	public function search_info()
	{
		$search_id = $_GET['search_id'];
		
		//得到寻找人信息详情
		$searchinfo = $this->_data_searchinfo->get_search_info_by_id($search_id);
		
		if(!$searchinfo)
		{
			$this->_alert('对不起，该信息不存在', '/index.php');
			exit;
		}	
		$this->_view->assign('reveal_path', $this->reveal_path);
		$this->_view->assign('searchinfo', $searchinfo);
	
		$this->_view->display('search_info.html');		
	}
	
	//发布信息列表
	public function publish_list()
	{				
		if(isset($_GET['t']) && $_GET['t'] == $this->pass)
		{
			$condition = ' is_del = 0 ORDER BY id DESC';
			$searchinfo_list = $this->_data_searchinfo->fetch_all_by_condition($condition);		
			
			$this->_view->assign('searchinfo_list', $searchinfo_list);
			
			$this->_view->display('publish_list.html');
		}
		else
		{
			$this->_alert('对不起，该网页不存在', '/index.php');
		}	
	}
	
	//删除信息
	public function del_public()
	{
		if(isset($_GET['id']) && $_GET['id'])
		{
			$id = $_GET['id'];
			$update_data['is_del'] = 1;
	
			if($this->_data_searchinfo->update_by_id($id, $update_data))
			{
				echo '<script>alert("删除成功"); location.href="/?act=publish_list"</script>';
			}
		}
	}
	
	//法律声明
	public function law_assert()
	{
		$this->_view->assign('top_fl', 'active');
		$this->_view->display('law_assert.html');
	}
	
	//邮件发送  
	public function send_email($tomail, $subject, $body) 
	{	
		global $email_config;
//print_r($email_config);exit;		
		$mail_host	= $email_config['mail_host'];	// SMTP 服务器  
		$send_user	= $email_config['send_user'];	// 用户名 
		$send_pass	= $email_config['send_pass'];	// 密码
		$fromName	= $email_config['fromName'];	// 发信人别名
		$frommail	= $email_config['frommail'];	// 发信人  
		$ccmail		='';
		
		$mail = new PHPMailer();    
		
		$mail->IsSMTP();					// 经smtp发送  
		$mail->Host     = $mail_host;       // SMTP 服务器  
		$mail->SMTPAuth = true;             // 打开SMTP 认证  
		$mail->Username = $send_user;    	// 用户名  
		$mail->Password = $send_pass;       // 密码  
		$mail->From     = $frommail;        // 发信人  
		$mail->FromName = $fromName;        // 发信人别名  
		$mail->AddAddress($tomail);         // 收信人  
		if(!empty($ccmail)){  
			$mail->AddCC($ccmail);          // cc收信人  
		}  
	
		$mail->WordWrap = 50;  
		$mail->CharSet = 'UTF-8'; 
		//$mail->Encoding = 'base64';
		$mail->IsHTML(true);                // 以html方式发送
	
		$mail->Subject  = $subject;         // 邮件标题
		$mail->Body     = $body;            // 邮件内容  
		//$mail->AltBody  =  "请使用HTML方式查看邮件。";  
		
		return $mail->Send();  
	}  
}




